<template>
  <el-dialog
    :title="formData.id?'编辑':'新增'"
    :visible="dialogVisible"
    @close="closeFn"
  >
    <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="角色名" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="角色详情" prop="title">
        <el-input v-model="formData.description" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer class="dialog-footer">
      <el-row type="flex" justify="center">
        <el-button size="small" type="primary" @click="addFn">确 定</el-button>
        <el-button size="small" @click="closeFn">取 消</el-button>
      </el-row>

    </template>
  </el-dialog>
</template>
<script>
import { addRoleList, editRoleListNumber, updateRoleList } from '@/api/roleAdmin'
export default {
  props: {
    dialogVisible: {
      default: false,
      type: Boolean
    }

  },
  data() {
    return {
      formData: {
        title: '',
        description: '',
        id: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      }

    }
  },
  methods: {
    // 关闭后清除表单
    closeFn() {
      this.$emit('update:dialogVisible', false)
      this.$refs.formData.resetFields()
      this.formData = {
        title: '',
        description: '',
        id: ''
      }
    },
    // 添加角色以及编辑橘色
    async addFn() {
      if (this.formData.id) {
        await updateRoleList(this.formData)
      } else {
        await addRoleList(this.formData)
      }
      this.closeFn()
      this.$emit('updateList')
    },
    // 编辑回填
    async editDataList(id) {
      const res = await editRoleListNumber(id)
      this.formData = res.data
    }
  }
}
</script>
<style scoped>

</style>
